﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using DHQGHN.DAL.Models.DanhMuc;
using DHQGHN.DAL.BO.DanhMuc;
using System.Configuration;
using System.ServiceModel;
using DHQGHN.DAL;

namespace DHQGHN.Services.DanhMuc
{
    public class AddressService : IAddressService
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(AddressService));
        private AddressBO addressBO;

        #region Constructors
        /// <summary>
        /// 
        /// </summary>
        public AddressService()
        {
            //string connString = ConfigurationManager.ConnectionStrings["DHQGHN_TDT.ConnectionString"].ConnectionString;
            addressBO = new AddressBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
        }
        #endregion

        #region Service Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listName"></param>
        /// <returns></returns>
        public IEnumerable<DAL.Models.DanhMuc.ADDRESS> GetAll()
        {
            try
            {
                return addressBO.GetAll();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault { 
                        ErrorMessage = ex.Message,
                        Source = ex.Source, 
                        Target = ex.TargetSite.ToString(), 
                        StackTrace = ex.StackTrace},
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listName"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIdx"></param>
        /// <returns></returns>
        public IEnumerable<DAL.Models.DanhMuc.ADDRESS> GetAllPaging(string pageSize, string pageIdx)
        {
            try
            {
                if (string.IsNullOrEmpty(pageSize)
                    || string.IsNullOrEmpty(pageIdx))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                int ipageSize, ipageIdx;
                if (int.TryParse(pageSize, out ipageSize) == false
                    || int.TryParse(pageIdx, out ipageIdx) == false)
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Tham số PageSize (hoặc PageIdx) sai định dạng!",
                        Description = "Tham số PageSize (hoặc PageIdx) sai định dạng!"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                return addressBO.GetAllPaging(ipageSize, ipageIdx).Data;
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listName"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public DAL.Models.DanhMuc.ADDRESS GetByID(string addressID)
        {
            try
            {
                string id = "-1";

                if (string.IsNullOrEmpty(addressID))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                //if (long.TryParse(addressID, out id) == false)
                //{
                //    Fault.ValidationFault fault = new Fault.ValidationFault()
                //    {
                //        Result = false,
                //        Message = "Tham số addressID sai định dạng!",
                //        Description = "Tham số addressID sai định dạng!"
                //    };

                //    throw new FaultException<Fault.ValidationFault>(fault);
                //}
                id = addressID;
                return addressBO.GetById(id);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="address"></param>
        public void AddAddress(ADDRESS address)
        {
            try
            {
                addressBO.Save(address);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="addressID"></param>
        public void DeleteAddress(string addressID)
        {
            try
            {
                addressBO.Delete(addressID);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="address"></param>
        public void EditAddress(ADDRESS address)
        {
            try
            {
                addressBO.Save(address);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        /// <summary>
        /// gopro, 20150508: get dia chi theo ma dia ban dieu tra
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        public IEnumerable<ADDRESS> GetByAreaCode(string areaCode)
        {
            try
            {
                if (string.IsNullOrEmpty(areaCode))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                return addressBO.GetByAreaCode(areaCode);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        /// <summary>
        /// datlq, 20150619, lay bang adminCode
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        public IEnumerable<ADDRESS> GetByAdminCode(string adminCode)
        {
            try
            {
                if (string.IsNullOrEmpty(adminCode))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                return addressBO.GetByAdminCode(adminCode);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        #endregion   
    }
}